/**
 * 将area.txt文本数据形成需要的json格式 并写入文件areaData.json
 */
let fs = require('fs')

fs.readFile('area.txt', 'utf-8', function (err, data) {
    if (err) {
        return console.error(err)
    }
    data = data
        .split('\n')
        .map(item => item.replace('\t\t\t\t\t\t', ''))
        .map(item => item.split('\t'))
    let dataArr = [], proviceObj = {}, cityObj = {}, areaObj = {}, preTwo, middleTwo
//     const groupObj = {
//                 label : '无分组',
//                 value : 0,
//                 level : 'group'
//                 }
    data.forEach((item, key) => {
        if (key < 2) {
            return true
        }
        if (!preTwo || preTwo !== item[0].substr(0, 2)) {
            preTwo = item[0].substr(0, 2)
            middleTwo = null
            proviceObj = {}
            proviceObj.label = item[1]
            proviceObj.value = item[0]
            proviceObj.level = 'p'
            proviceObj.children = []
            // proviceObj.code = item[0]
            dataArr.push(proviceObj)
        } else {
            if (!middleTwo || middleTwo !== item[0].substr(2, 2)) {
                middleTwo = item[0].substr(2, 2)
                cityObj = {}
                cityObj.label = item[1]
                cityObj.value = item[0]
                cityObj.level = 'c'
                cityObj.children = []
                // cityObj.code = item[0]
                proviceObj.children.push(cityObj)
            } else {
                areaObj = {}
                areaObj.label = item[1]
                areaObj.value = item[0]
                areaObj.level = 'd'
//                 areaObj.children = []
//                 areaObj.children.push(groupObj)
                cityObj.children.push(areaObj)
            }
        }
    })

    fs.writeFile('areaData2.json', JSON.stringify(dataArr), function (err) {
        if (err) {
            return console.error(err)
        }
    })
})
